<?php
namespace Tbf\Bundle\SerializerBundle;
use Tbf\Component\Arrays\ArrayReaderInterface;

class SerializeArrayReader implements ArrayReaderInterface{
    /** @var \Tbf\Bundle\SerializerBundle\Serializer  */
    protected $serializer;
    /** @var array $data []interface{} */
    protected $data;
    /** @var string */
    protected $serialize_group=null;
    /**
     * @param Serializer $serializer
     * @param array $data
     */
    function __construct(Serializer $serializer,array $data=array()){
        $this->serializer = $serializer;
        $this->data = $data;
        reset($this->data);
    }

    /**
     * @param array $data
     * @return $this
     */
    function setData(array $data){
        $this->data = $data;
        reset($this->data);
        return $this;
    }
    function setSerializeGroup($group){
        $this->serialize_group = $group;
        return $this;
    }
    /**
     * 读出一个一维array数组
     * 如果返回值为null表示没有数据了
     * @return array data 数据
     */
    public function readRow()
    {
        if (key($this->data)===null){
            return null;
        }
        $obj = current($this->data);
        next($this->data);
        return $this->serializer->toArray($obj,$this->serialize_group);
    }
}